package com.yanqu.road.server.manager.cross.battle.dunhuangshiku;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.CrossActivityDetail;
import com.yanqu.road.entity.activity.dunhuangshiku.DunHuangShiKuKillLogData;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.server.manager.douluodalu.CrossDouLouDaLuActivity;
import com.yanqu.road.server.manager.douluodalu.CrossDouLuoDaLuMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author : Cxp
 * @Time : 2024-08-23
 * @Description : 敦煌石窟活动分组
 */

public class DunHuangShiKuActivityGroup {
    private int activityId;

    private Map<Integer, DunHuangShiKuActivity> activityMap = new ConcurrentHashMap<>();

    public DunHuangShiKuActivityGroup(int activityId) {
        this.activityId = activityId;
    }

    public int getActivityId() {
        return activityId;
    }

    public void setActivityMap(Map<Integer, DunHuangShiKuActivity> activityMap) {
        this.activityMap = activityMap;
    }

    public Map<Integer, DunHuangShiKuActivity> getActivityMap() {
        return activityMap;
    }

    public DunHuangShiKuActivity getActivityOrNull(int groupId) {
        return activityMap.get(groupId);
    }

    public DunHuangShiKuActivity getActivity(int groupId) {
        if (groupId == UnionActivityGroupHelper.NO_GROUP_ID) {
            return null;
        }
        DunHuangShiKuActivity activity = activityMap.get(groupId);
        if (activity == null) {
            // 读配置 获取活动信息 保证有活动信息
            ActivityInfo activityInfo = ActivityBussiness.getActivityInfoByActivityId(activityId);
            if (activityInShowTime(activityInfo)) {
                activity = new DunHuangShiKuActivity(activityInfo, groupId);
                activityMap.put(groupId, activity);
            }
        } else {
            CrossActivityDetail crossActivityDetail = activity.getCrossActivityDetail();
            if (DateHelper.getDiffDayNum(crossActivityDetail.getParam1()) > 0) {
                // 重置所有玩家区域
                synchronized (activity) {
                    // param1表示上次刷新活动区域时间
                    if (DateHelper.getDiffDayNum(crossActivityDetail.getParam1()) > 0) {
                        for (DunHuangShiKuUser item : activity.userMap.values()) {
                            activity.dropRegion(item, activity.regionConfigList.get(0).getId(), true);
                            // 重置日志
                            List<DunHuangShiKuKillLogData> logDataList = item.getUserLogDataList(false);
                            if (logDataList != null) {
                                logDataList.clear();
                            }
                        }
                        activity.region3UserList.clear();
                        // 重置公共日志
                        if (activity.killLogList.size() > 0) {
                            activity.killLogList.clear();
                        }
                        // 重置玩家私有日志
                        for (DunHuangShiKuUser item : activity.userMap.values()) {
                            List<DunHuangShiKuKillLogData> list = item.getUserLogDataList(false);
                            if (list != null) {
                                list.clear();
                            }
                        }
                        crossActivityDetail.setParam1(System.currentTimeMillis() / 1000);
                    }
                }
            }
        }
        return activity;
    }

    private boolean activityInShowTime(ActivityInfo activityInfo) {
        if (null == activityInfo) {
            return false;
        }
        long nowTime = System.currentTimeMillis() / 1000;
        return nowTime >= activityInfo.getBeginShowTime() && nowTime < activityInfo.getEndShowTime();
    }

    public void save() {
        for (DunHuangShiKuActivity activity : activityMap.values()) {
            try {
                activity.save();
            } catch (Exception e) {
                CrossDouLuoDaLuMgr.getLogger().error("activity {} groupId {} save err", activity.getActivityId(), activity.getGroupId(), e.getCause());
            }

        }
    }
}
